package com.hdoit.ares.web.cbase.busutils;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;

import com.hdoit.ares.selfcommon.enums.cust.CustomerLevel;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.logical.wechat.model.CustomerQrcode;
import com.tennetcn.common.logical.wechat.service.ICustomerQrcodeService;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.wechat.WechatManager;

@Component
public class CustomerQrcodeUtil {
	private ICustomerQrcodeService customerQrcodeService;
	
	public ICustomerQrcodeService getCustomerQrcodeService() {
		return customerQrcodeService;
	}
	
	@Resource
	public void setCustomerQrcodeService(ICustomerQrcodeService customerQrcodeService) {
		this.customerQrcodeService = customerQrcodeService;
	}
	
	public String getQrcodeUrl(String customerId,String level,String sceneCode){
		/*查询当前登录人在链接表中有没有*/
		CustomerQrcode customerQrcode = customerQrcodeService.queryModel(customerId);
		
		/*微信公共号开发*/
		WxMpService wxMpService=WechatManager.getInstance().getWxMpService();
		String qrUrl = null;
		
		if (customerQrcode!=null&&((new Date().getTime() - customerQrcode.getCreateDate().getTime()) < customerQrcode.getDuration()*1000 || level.equals(CustomerLevel.highMaker))) {
			/**
			 * 条件：查询到了数据 && (链接未过期  || 身份是高创)
			 * 行为：直接取出数据来用。
			 */			
			/*提取url*/
			qrUrl = customerQrcode.getQrUrl();
		}else if(customerQrcode!=null&&!level.equals(CustomerLevel.highMaker)) {
			/**
			 * 条件：查询到了数据 && 链接过期了
			 * 行为：更新createDate、qrUrl字段
			 */
			qrUrl = generateQrUrl(level, wxMpService, sceneCode);
			customerQrcode.setCreateDate(DateUtils.getCurrent());
			customerQrcode.setQrUrl(qrUrl);
			
			customerQrcode.setModelStatus(ModelStatus.update);
			customerQrcodeService.applyChange(customerQrcode);
		}else {
			/**
			 * 条件：没有查询到数据
			 * 行为：新建一条数据。
			 */
			int duration = (CustomerLevel.highMaker.equals(level))?0:59*60*24*7;
			qrUrl = generateQrUrl(level, wxMpService, sceneCode);
			customerQrcode = new CustomerQrcode();
			customerQrcode.setCreateDate(new Date());
			customerQrcode.setCustomerId(customerId);
			customerQrcode.setDuration(duration);
			customerQrcode.setId(CommonUtils.getUUID());
			customerQrcode.setQrUrl(qrUrl);
			
			customerQrcode.setModelStatus(ModelStatus.add);
			customerQrcodeService.applyChange(customerQrcode);
		}
		return qrUrl;
	}
	
	/**
	 * 根据身份生成二维码链接的门票
	 * @param model
	 * @param level
	 * @param wxMpService
	 * @param sceneCode
	 * @return
	 */
	private String generateQrUrl(String level, WxMpService wxMpService, String sceneCode){
		String qrUrl = null;
		try {
			if (CustomerLevel.highMaker.equals(level)) {
					WxMpQrCodeTicket wxMpQrCodeTicket=wxMpService.getQrcodeService().qrCodeCreateLastTicket(sceneCode);
					qrUrl = wxMpQrCodeTicket.getUrl();
			}else {
				WxMpQrCodeTicket wxMpQrCodeTicket=wxMpService.getQrcodeService().qrCodeCreateTmpTicket(sceneCode, 60*60*24*7);
				qrUrl = wxMpQrCodeTicket.getUrl();
			}
		} catch (WxErrorException e) {
			e.printStackTrace();
		}
		
		return qrUrl;
	}
}
